package com.pll.controller.MerchantAuthorization;

import com.pll.util.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;


import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * @BelongsProject: fore-phases
 * @BelongsPackage: com.pll.controller.MerchantAuthorization
 * @Author: 康思捷
 * @CreateTime: 2025-06-13 10:04
 * @Description: TODO
 * @Version: 1.0
 */
public class TextRecognition {
    public static void main(String[] args) {
        // 1. 本地图片路径（注意转义Windows路径）
        String imagePath = "C:\\\\Users\\\\moya\\\\Desktop\\\\四阶段项目\\\\2.jpg";

        // 2. 阿里云API配置
        String host = "https://bizlicense.market.alicloudapi.com";
        String path = "/rest/160601/ocr/ocr_business_license.json";
        String appcode = "1c396eacc15f40bb822b4d27b31c872d"; // 替换为你的真实AppCode

        // 3. 构造请求头
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", "APPCODE " + appcode);
        headers.put("Content-Type", "application/json; charset=UTF-8");

        try {
            // 4. 读取图片并转换为Base64
            byte[] imageBytes = Files.readAllBytes(Paths.get(imagePath));
            String base64Image = Base64.getEncoder().encodeToString(imageBytes);

            // 5. 构造请求体
            String bodys = "{\"image\":\"" + base64Image + "\"}";

            // 6. 发送请求
            HttpResponse response = HttpUtils.doPost(host, path, "POST", headers, new HashMap<>(), bodys);

            // 7. 打印完整结果
            System.out.println("响应状态：" + response.getStatusLine());
            System.out.println("识别结果：\n" + EntityUtils.toString(response.getEntity()));

        } catch (Exception e) {
            System.err.println("发生错误：" + e.getMessage());
            e.printStackTrace();
        }
    }
}



